package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class MainActivity extends AppCompatActivity {
    private Double number1;
    private Double number2;
    private Basic lastBasicOperator;
    private TextView inputTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.inputTextView = findViewById(R.id.inputTextView);;

        setNumberButtons();
        setBasicOperatorButtons();
        setOtherButtons();
        setFunctionOperatorButtons();
    }

    private void setFunctionOperatorButtons() {
        Set<FunButton> functionOperatorButtons = new HashSet<>();

        functionOperatorButtons.add(new FunButton(new Fun() {
            @Override
            public double calc(double v) {
                return Math.pow(v, 2);
            }
        }, findViewById(R.id.xPinFangBtn)));
        functionOperatorButtons.add(new FunButton(new Fun() {
            @Override
            public double calc(double v) {
                return 1 / v;
            }
        }, findViewById(R.id.f1ChuYiXBtn)));
        functionOperatorButtons.add(new FunButton(new Fun() {
            @Override
            public double calc(double v) {
                return Math.pow(v, 1D / 2);
            }
        }, findViewById(R.id.f2genHaoXBtn)));

        for (FunButton functionOperatorButton : functionOperatorButtons) {
            Button button = functionOperatorButton.getButton();

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    double inputNumber;

                    try {
                        inputNumber = Double.parseDouble(inputTextView.getText().toString());
                    } catch (Exception e) {
                        return;
                    }

                    double result = functionOperatorButton.getFunctionOperator().calc(inputNumber);

                    inputTextView.setText(String.valueOf(result));
                }
            });
        }
    }

    private void setOtherButtons() {
        findViewById(R.id.dengYuBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (lastBasicOperator == null) {
                    return;
                }

                if (number2 == null) {
                    try {
                        number2 = Double.parseDouble(inputTextView.getText().toString());
                    } catch (Exception e) {
                        return;
                    }
                }

                double result = lastBasicOperator.calc(number1, number2);

                inputTextView.setText(
                        Utils.subZeroAndDot(
                                String.valueOf(
                                        result)));
                number1 = result;
            }
        });

        findViewById(R.id.cBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = inputTextView.getText().toString();

                if (!text.isEmpty()) {
                    String tmp = text.substring(0, text.length() - 1);

                    inputTextView.setText(tmp.endsWith(".") ? tmp.substring(0, tmp.length() - 1) : tmp);
                }
            }
        });
        findViewById(R.id.dianBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = inputTextView.getText().toString();

                if (text.contains(".")) {
                    return;
                }

                if (!text.isEmpty()) {
                    inputTextView.setText(text + ".");
                }
            }
        });
        findViewById(R.id.ceBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inputTextView.setText("");
            }
        });
    }

    private void setBasicOperatorButtons() {
        Set<BasicButton> basicOperatorButtons = new HashSet<>();

        basicOperatorButtons.add(new BasicButton(findViewById(R.id.jiaBtn), new Basic() {
            @Override
            public double calc(double v1, double v2) {
                return v1 + v2;
            }
        }));
        basicOperatorButtons.add(new BasicButton(findViewById(R.id.jianBtn), new Basic() {
            @Override
            public double calc(double v1, double v2) {
                return v1 - v2;
            }
        }));
        basicOperatorButtons.add(new BasicButton(findViewById(R.id.chengYiBtn), new Basic() {
            @Override
            public double calc(double v1, double v2) {
                return v1 * v2;
            }
        }));
        basicOperatorButtons.add(new BasicButton(findViewById(R.id.chuYiBtn), new Basic() {
            @Override
            public double calc(double v1, double v2) {
                return v1 / v2;
            }
        }));

        for (BasicButton basicOperatorButton : basicOperatorButtons) {
            Button button = basicOperatorButton.getButton();

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    number2 = null;

                    try {
                        number1 = Double.parseDouble(inputTextView.getText().toString());
                    } catch (Exception e) {
                        return;
                    }

                    lastBasicOperator = basicOperatorButton.getBasicOperator();
                    inputTextView.setText("");
                }
            });
        }
    }

    private void setNumberButtons() {
        for (Object objBtn : Arrays.asList(
                findViewById(R.id.n0Btn),
                findViewById(R.id.n1Btn),
                findViewById(R.id.n2Btn),
                findViewById(R.id.n3Btn),
                findViewById(R.id.n4Btn),
                findViewById(R.id.n5Btn),
                findViewById(R.id.n6Btn),
                findViewById(R.id.n7Btn),
                findViewById(R.id.n8Btn),
                findViewById(R.id.n9Btn)
        )) {
            Button btn = (Button) objBtn;

            btn.setOnClickListener(view -> {
                String text = inputTextView.getText().toString();

                if (text.equals("0")) {
                    text = "";
                }

                StringBuilder stringBuilder = new StringBuilder();

                stringBuilder.append(text);
                stringBuilder.append(btn.getText());

                inputTextView.setText(stringBuilder);
            });
        }
    }
}